home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / binutils / objcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-14  |  25.9 KB  |  1,064 lines

  1. /* objcopy.c -- copy object file from input to output, optionally massaging it.
  2.    Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Binutils.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bucomm.h"
  23. #include <getopt.h>
  24. #include "libiberty.h"
  25.  
  26. static void setup_section ();
  27. static void copy_section ();
  28. static void mark_symbols_used_in_relocations ();
  29.  
  30. #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
  31.  
  32. static asymbol **isympp = NULL;    /* Input symbols */
  33. static asymbol **osympp = NULL;    /* Output symbols that survive stripping */
  34.  
  35. /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes.  */
  36. static int copy_byte = -1;
  37. static int interleave = 4;
  38.  
  39. static boolean verbose;        /* Print file and target names. */
  40. static int status = 0;        /* Exit status.  */
  41.  
  42. enum strip_action
  43.   {
  44.     strip_undef,
  45.     strip_none,            /* don't strip */
  46.     strip_debug,        /* strip all debugger symbols */
  47.     strip_all            /* strip all symbols */
  48.   };
  49.  
  50. /* Which symbols to remove. */
  51. static enum strip_action strip_symbols;
  52.  
  53. enum locals_action
  54.   {
  55.     locals_undef,
  56.     locals_start_L,        /* discard locals starting with L */
  57.     locals_all            /* discard all locals */
  58.   };
  59.  
  60. /* Which local symbols to remove.  Overrides strip_all.  */
  61. static enum locals_action discard_locals;
  62.  
  63. /* List of sections to remove.  */
  64.  
  65. struct section_list
  66. {
  67.   struct section_list *next;
  68.   const char *name;
  69. };
  70.  
  71. static struct section_list *remove_sections;
  72.  
  73. /* Options to handle if running as "strip".  */
  74.  
  75. static struct option strip_options[] =
  76. {
  77.   {"discard-all", no_argument, 0, 'x'},
  78.   {"discard-locals", no_argument, 0, 'X'},
  79.   {"format", required_argument, 0, 'F'}, /* Obsolete */
  80.   {"help", no_argument, 0, 'h'},
  81.   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  82.   {"input-target", required_argument, 0, 'I'},
  83.   {"output-format", required_argument, 0, 'O'},    /* Obsolete */
  84.   {"output-target", required_argument, 0, 'O'},
  85.   {"remove-section", required_argument, 0, 'R'},
  86.   {"strip-all", no_argument, 0, 's'},
  87.   {"strip-debug", no_argument, 0, 'S'},
  88.   {"target", required_argument, 0, 'F'},
  89.   {"verbose", no_argument, 0, 'v'},
  90.   {"version", no_argument, 0, 'V'},
  91.   {0, no_argument, 0, 0}
  92. };
  93.  
  94. /* Options to handle if running as "objcopy".  */
  95.  
  96. static struct option copy_options[] =
  97. {
  98.   {"byte", required_argument, 0, 'b'},
  99.   {"discard-all", no_argument, 0, 'x'},
  100.   {"discard-locals", no_argument, 0, 'X'},
  101.   {"format", required_argument, 0, 'F'}, /* Obsolete */
  102.   {"help", no_argument, 0, 'h'},
  103.   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  104.   {"input-target", required_argument, 0, 'I'},
  105.   {"interleave", required_argument, 0, 'i'},
  106.   {"output-format", required_argument, 0, 'O'},    /* Obsolete */
  107.   {"output-target", required_argument, 0, 'O'},
  108.   {"remove-section", required_argument, 0, 'R'},
  109.   {"strip-all", no_argument, 0, 'S'},
  110.   {"strip-debug", no_argument, 0, 'g'},
  111.   {"target", required_argument, 0, 'F'},
  112.   {"verbose", no_argument, 0, 'v'},
  113.   {"version", no_argument, 0, 'V'},
  114.   {0, no_argument, 0, 0}
  115. };
  116.  
  117. /* IMPORTS */
  118. extern char *program_name;
  119. extern char *program_version;
  120.  
  121. /* This flag distinguishes between strip and objcopy:
  122.    1 means this is 'strip'; 0 means this is 'objcopy'.
  123.    -1 means if we should use argv[0] to decide. */
  124. extern int is_strip;
  125.  
  126.  
  127. static void
  128. copy_usage (stream, status)
  129.      FILE *stream;
  130.      int status;
  131. {
  132.   fprintf (stream, "\
  133. Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
  134.        [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
  135.        [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
  136.        [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
  137.        [--remove-section=section] [--verbose] [--version] [--help]\n\
  138.        in-file [out-file]\n",
  139.        program_name);
  140.   exit (status);
  141. }
  142.  
  143. static void
  144. strip_usage (stream, status)
  145.      FILE *stream;
  146.      int status;
  147. {
  148.   fprintf (stream, "\
  149. Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
  150.        [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
  151.        [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\
  152.        [--remove-section=section] [--verbose] [--version] [--help] file...\n",
  153.        program_name);
  154.   exit (status);
  155. }
  156.  
  157.  
  158. /* Return the name of a temporary file in the same directory as FILENAME.  */
  159.  
  160. static char *
  161. make_tempname (filename)
  162.      char *filename;
  163. {
  164.   static char template[] = "stXXXXXX";
  165.   char *tmpname;
  166.   char *slash = strrchr (filename, '/');
  167.  
  168.   if (slash != (char *) NULL)
  169.     {
  170.       *slash = 0;
  171.       tmpname = xmalloc (strlen (filename) + sizeof (template) + 1);
  172.       strcpy (tmpname, filename);
  173.       strcat (tmpname, "/");
  174.       strcat (tmpname, template);
  175.       mktemp (tmpname);
  176.       *slash = '/';
  177.     }
  178.   else
  179.     {
  180.       tmpname = xmalloc (sizeof (template));
  181.       strcpy (tmpname, template);
  182.       mktemp (tmpname);
  183.     }
  184.   return tmpname;
  185. }
  186.  
  187. /* Choose which symbol entries to copy; put the result in OSYMS.
  188.    We don't copy in place, because that confuses the relocs.
  189.    Return the number of symbols to print.  */
  190.  
  191. static unsigned int
  192. filter_symbols (abfd, osyms, isyms, symcount)
  193.      bfd *abfd;
  194.      asymbol **osyms, **isyms;
  195.      long symcount;
  196. {
  197.   register asymbol **from = isyms, **to = osyms;
  198.   long src_count = 0, dst_count = 0;
  199.  
  200.   for (; src_count < symcount; src_count++)
  201.     {
  202.       asymbol *sym = from[src_count];
  203.       flagword flags = sym->flags;
  204.       int keep;
  205.  
  206.       if ((flags & BSF_GLOBAL)    /* Keep if external.  */
  207.       || (flags & BSF_KEEP)    /* Keep if used in a relocation.  */
  208.       || bfd_is_und_section (bfd_get_section (sym))
  209.       || bfd_is_com_section (bfd_get_section (sym)))
  210.     keep = 1;
  211.       else if ((flags & BSF_DEBUGGING) != 0)    /* Debugging symbol.  */
  212.     keep = strip_symbols != strip_debug;
  213.       else            /* Local symbol.  */
  214.     keep = discard_locals != locals_all
  215.       && (discard_locals != locals_start_L ||
  216.           ! bfd_is_local_label (abfd, sym));
  217.       if (keep)
  218.     to[dst_count++] = sym;
  219.     }
  220.  
  221.   return dst_count;
  222. }
  223.  
  224. /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
  225.    Adjust *SIZE.  */
  226.  
  227. void
  228. filter_bytes (memhunk, size)
  229.      char *memhunk;
  230.      bfd_size_type *size;
  231. {
  232.   char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
  233.  
  234.   for (; from < end; from += interleave)
  235.     *to++ = *from;
  236.   *size /= interleave;
  237. }
  238.  
  239. /* Copy object file IBFD onto OBFD.  */
  240.  
  241. static void
  242. copy_object (ibfd, obfd)
  243.      bfd *ibfd;
  244.      bfd *obfd;
  245. {
  246.   long symcount;
  247.  
  248.   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  249.     {
  250.       nonfatal (bfd_get_filename (obfd));
  251.     }
  252.  
  253.   if (verbose)
  254.     printf ("copy from %s(%s) to %s(%s)\n",
  255.         bfd_get_filename(ibfd), bfd_get_target(ibfd),
  256.         bfd_get_filename(obfd), bfd_get_target(obfd));
  257.  
  258.   if (!bfd_set_start_address (obfd, bfd_get_start_address (ibfd))
  259.       || !bfd_set_file_flags (obfd,
  260.                   (bfd_get_file_flags (ibfd)
  261.                    & bfd_applicable_file_flags (obfd))))
  262.     {
  263.       nonfatal (bfd_get_filename (ibfd));
  264.     }
  265.  
  266.   /* Copy architecture of input file to output file */
  267.   if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  268.               bfd_get_mach (ibfd)))
  269.     {
  270.       fprintf (stderr, "Output file cannot represent architecture %s\n",
  271.            bfd_printable_arch_mach (bfd_get_arch (ibfd),
  272.                     bfd_get_mach (ibfd)));
  273.     }
  274.   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  275.     {
  276.       nonfatal (bfd_get_filename(ibfd));
  277.     }
  278.  
  279.   if (isympp)
  280.     free (isympp);
  281.   if (osympp != isympp)
  282.     free (osympp);
  283.  
  284.   /* bfd mandates that all output sections be created and sizes set before
  285.      any output is done.  Thus, we traverse all sections multiple times.  */
  286.   bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
  287.  
  288.   /* Symbol filtering must happen after the output sections have
  289.      been created, but before their contents are set.  */
  290.   if (strip_symbols == strip_all && discard_locals == locals_undef)
  291.     {
  292.       osympp = isympp = NULL;
  293.       symcount = 0;
  294.     }
  295.   else
  296.     {
  297.       long symsize;
  298.  
  299.       symsize = bfd_get_symtab_upper_bound (ibfd);
  300.       if (symsize < 0)
  301.     {
  302.       nonfatal (bfd_get_filename (ibfd));
  303.     }
  304.  
  305.       osympp = isympp = (asymbol **) xmalloc (symsize);
  306.       symcount = bfd_canonicalize_symtab (ibfd, isympp);
  307.       if (symcount < 0)
  308.     {
  309.       nonfatal (bfd_get_filename (ibfd));
  310.     }
  311.  
  312.       if (strip_symbols == strip_debug || discard_locals != locals_undef)
  313.     {
  314.       /* Mark symbols used in output relocations so that they
  315.          are kept, even if they are local labels or static symbols.
  316.  
  317.          Note we iterate over the input sections examining their
  318.          relocations since the relocations for the output sections
  319.          haven't been set yet.  mark_symbols_used_in_relocations will
  320.          ignore input sections which have no corresponding output
  321.          section.  */
  322.       bfd_map_over_sections (ibfd,
  323.                  mark_symbols_used_in_relocations,
  324.                  (void *)isympp);
  325.       osympp = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
  326.       symcount = filter_symbols (ibfd, osympp, isympp, symcount);
  327.     }
  328.     }
  329.  
  330.   bfd_set_symtab (obfd, osympp, symcount);
  331.  
  332.   /* This has to happen after the symbol table has been set.  */
  333.   bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
  334.  
  335.   /* Allow the BFD backend to copy any private data it understands
  336.      from the input BFD to the output BFD.  This is done last to
  337.      permit the routine to look at the filtered symbol table, which is
  338.      important for the ECOFF code at least.  */
  339.   if (!bfd_copy_private_bfd_data (ibfd, obfd))
  340.     {
  341.       fprintf (stderr, "%s: %s: error copying private BFD data: %s\n",
  342.            program_name, bfd_get_filename (obfd),
  343.            bfd_errmsg (bfd_get_error ()));
  344.       status = 1;
  345.       return;
  346.     }
  347. }
  348.  
  349. static char *
  350. cat (a, b, c)
  351.      char *a;
  352.      char *b;
  353.      char *c;
  354. {
  355.   size_t size = strlen (a) + strlen (b) + strlen (c);
  356.   char *r = xmalloc (size + 1);
  357.  
  358.   strcpy (r, a);
  359.   strcat (r, b);
  360.   strcat (r, c);
  361.   return r;
  362. }
  363.  
  364. /* Read each archive element in turn from IBFD, copy the
  365.    contents to temp file, and keep the temp file handle.  */
  366.  
  367. static void
  368. copy_archive (ibfd, obfd, output_target)
  369.      bfd *ibfd;
  370.      bfd *obfd;
  371.      char *output_target;
  372. {
  373.   bfd **ptr = &obfd->archive_head;
  374.   bfd *this_element;
  375.   char *dir = make_tempname (bfd_get_filename (obfd));
  376.  
  377.   /* Make a temp directory to hold the contents.  */
  378.   mkdir (dir, 0700);
  379.   obfd->has_armap = ibfd->has_armap;
  380.  
  381.   this_element = bfd_openr_next_archived_file (ibfd, NULL);
  382.   ibfd->archive_head = this_element;
  383.   while (this_element != (bfd *) NULL)
  384.     {
  385.       /* Create an output file for this member.  */
  386.       char *output_name = cat (dir, "/", bfd_get_filename(this_element));
  387.       bfd *output_bfd = bfd_openw (output_name, output_target);
  388.  
  389.       if (output_bfd == (bfd *) NULL)
  390.     {
  391.       nonfatal (output_name);
  392.     }
  393.       if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  394.     {
  395.       nonfatal (bfd_get_filename (obfd));
  396.     }
  397.  
  398.       if (bfd_check_format (this_element, bfd_object) == true)
  399.     {
  400.       copy_object (this_element, output_bfd);
  401.     }
  402.  
  403.       bfd_close (output_bfd);
  404.       /* Open the newly output file and attatch to our list.  */
  405.       output_bfd = bfd_openr (output_name, output_target);
  406.  
  407.       /* Mark it for deletion.  */
  408.       *ptr = output_bfd;
  409.       ptr = &output_bfd->next;
  410.       this_element->next = bfd_openr_next_archived_file (ibfd, this_element);
  411.       this_element = this_element->next;
  412.     }
  413.   *ptr = (bfd *) NULL;
  414.  
  415.   if (!bfd_close (obfd))
  416.     {
  417.       nonfatal (bfd_get_filename (obfd));
  418.     }
  419.  
  420.   /* Delete all the files that we opened.
  421.      Construct their names again, unfortunately, but
  422.      we're about to exit anyway.  */
  423.   for (this_element = ibfd->archive_head;
  424.        this_element != (bfd *) NULL;
  425.        this_element = this_element->next)
  426.     {
  427.       unlink (cat (dir, "/", bfd_get_filename (this_element)));
  428.     }
  429.   rmdir (dir);
  430.   if (!bfd_close (ibfd))
  431.     {
  432.       nonfatal (bfd_get_filename (ibfd));
  433.     }
  434. }
  435.  
  436. /* The top-level control.  */
  437.  
  438. static void
  439. copy_file (input_filename, output_filename, input_target, output_target)
  440.      char *input_filename;
  441.      char *output_filename;
  442.      char *input_target;
  443.      char *output_target;
  444. {
  445.   bfd *ibfd;
  446.   char **matching;
  447.  
  448.   /* To allow us to do "strip *" without dying on the first
  449.      non-object file, failures are nonfatal.  */
  450.  
  451.   ibfd = bfd_openr (input_filename, input_target);
  452.   if (ibfd == NULL)
  453.     {
  454.       nonfatal (input_filename);
  455.     }
  456.  
  457.   if (bfd_check_format (ibfd, bfd_archive))
  458.     {
  459.       bfd *obfd;
  460.  
  461.       /* bfd_get_target does not return the correct value until
  462.          bfd_check_format succeeds.  */
  463.       if (output_target == NULL)
  464.     output_target = bfd_get_target (ibfd);
  465.  
  466.       obfd = bfd_openw (output_filename, output_target);
  467.       if (obfd == NULL)
  468.     {
  469.       nonfatal (output_filename);
  470.     }
  471.       copy_archive (ibfd, obfd, output_target);
  472.     }
  473.   else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
  474.     {
  475.       bfd *obfd;
  476.  
  477.       /* bfd_get_target does not return the correct value until
  478.          bfd_check_format succeeds.  */
  479.       if (output_target == NULL)
  480.     output_target = bfd_get_target (ibfd);
  481.  
  482.       obfd = bfd_openw (output_filename, output_target);
  483.       if (obfd == NULL)
  484.     {
  485.       nonfatal (output_filename);
  486.     }
  487.  
  488.       copy_object (ibfd, obfd);
  489.  
  490.       if (!bfd_close (obfd))
  491.     {
  492.       nonfatal (output_filename);
  493.     }
  494.  
  495.       if (!bfd_close (ibfd))
  496.     {
  497.       nonfatal (input_filename);
  498.     }
  499.     }
  500.   else
  501.     {
  502.       bfd_nonfatal (input_filename);
  503.       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  504.     {
  505.       list_matching_formats (matching);
  506.       free (matching);
  507.     }
  508.       status = 1;
  509.     }
  510. }
  511.  
  512. /* Create a section in OBFD with the same name and attributes
  513.    as ISECTION in IBFD.  */
  514.  
  515. static void
  516. setup_section (ibfd, isection, obfd)
  517.      bfd *ibfd;
  518.      sec_ptr isection;
  519.      bfd *obfd;
  520. {
  521.   struct section_list *p;
  522.   sec_ptr osection;
  523.   char *err;
  524.  
  525.   if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
  526.       && (strip_symbols == strip_debug
  527.       || strip_symbols == strip_all
  528.       || discard_locals == locals_all))
  529.     return;
  530.  
  531.   for (p = remove_sections; p != NULL; p = p->next)
  532.     if (strcmp (p->name, bfd_section_name (ibfd, isection)) == 0)
  533.       return;
  534.  
  535.   osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
  536.   if (osection == NULL)
  537.     {
  538.       err = "making";
  539.       goto loser;
  540.     }
  541.  
  542.   if (!bfd_set_section_size (obfd,
  543.                  osection,
  544.                  bfd_section_size (ibfd, isection)))
  545.     {
  546.       err = "size";
  547.       goto loser;
  548.     }
  549.  
  550.   if (bfd_set_section_vma (obfd,
  551.                osection,
  552.                bfd_section_vma (ibfd, isection))
  553.       == false)
  554.     {
  555.       err = "vma";
  556.       goto loser;
  557.     }
  558.  
  559.   if (bfd_set_section_alignment (obfd,
  560.                  osection,
  561.                  bfd_section_alignment (ibfd, isection))
  562.       == false)
  563.     {
  564.       err = "alignment";
  565.       goto loser;
  566.     }
  567.  
  568.   if (!bfd_set_section_flags (obfd, osection,
  569.                   bfd_get_section_flags (ibfd, isection)))
  570.     {
  571.       err = "flags";
  572.       goto loser;
  573.     }
  574.  
  575.   /* This used to be mangle_section; we do here to avoid using
  576.      bfd_get_section_by_name since some formats allow multiple
  577.      sections with the same name.  */
  578.   isection->output_section = osection;
  579.   isection->output_offset = 0;
  580.  
  581.   /* Allow the BFD backend to copy any private data it understands
  582.      from the input section to the output section.  */
  583.   if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
  584.     {
  585.       err = "private data";
  586.       goto loser;
  587.     }
  588.  
  589.   /* All went well */
  590.   return;
  591.  
  592. loser:
  593.   fprintf (stderr, "%s: %s: section `%s': error in %s: %s\n",
  594.        program_name,
  595.        bfd_get_filename (ibfd), bfd_section_name (ibfd, isection),
  596.        err, bfd_errmsg (bfd_get_error ()));
  597.   status = 1;
  598. }
  599.  
  600. /* Copy the data of input section ISECTION of IBFD
  601.    to an output section with the same name in OBFD.
  602.    If stripping then don't copy any relocation info.  */
  603.  
  604. static void
  605. copy_section (ibfd, isection, obfd)
  606.      bfd *ibfd;
  607.      sec_ptr isection;
  608.      bfd *obfd;
  609. {
  610.   struct section_list *p;
  611.   arelent **relpp;
  612.   long relcount;
  613.   sec_ptr osection;
  614.   bfd_size_type size;
  615.  
  616.   if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
  617.       && (strip_symbols == strip_debug
  618.       || strip_symbols == strip_all
  619.       || discard_locals == locals_all))
  620.     {
  621.       return;
  622.     }
  623.  
  624.   for (p = remove_sections; p != NULL; p = p->next)
  625.     if (strcmp (p->name, bfd_section_name (ibfd, isection)) == 0)
  626.       return;
  627.  
  628.   osection = isection->output_section;
  629.   size = bfd_get_section_size_before_reloc (isection);
  630.  
  631.   if (size == 0 || osection == 0)
  632.     return;
  633.  
  634.   if (strip_symbols == strip_all)
  635.     bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
  636.   else
  637.     {
  638.       long relsize;
  639.  
  640.       relsize = bfd_get_reloc_upper_bound (ibfd, isection);
  641.       if (relsize < 0)
  642.     {
  643.       nonfatal (bfd_get_filename (ibfd));
  644.     }
  645.       if (relsize == 0)
  646.     bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
  647.       else
  648.     {
  649.       relpp = (arelent **) xmalloc (relsize);
  650.       relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
  651.       if (relcount < 0)
  652.         {
  653.           nonfatal (bfd_get_filename (ibfd));
  654.         }
  655.       bfd_set_reloc (obfd, osection, relpp, relcount);
  656.     }
  657.     }
  658.  
  659.   isection->_cooked_size = isection->_raw_size;
  660.   isection->reloc_done = true;
  661.  
  662.   if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
  663.     {
  664.       PTR memhunk = (PTR) xmalloc ((unsigned) size);
  665.  
  666.       if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
  667.                      size))
  668.     {
  669.       nonfatal (bfd_get_filename (ibfd));
  670.     }
  671.  
  672.       if (copy_byte >= 0) 
  673.         {
  674.       filter_bytes (memhunk, &size);
  675.               /* The section has gotten smaller. */
  676.           if (!bfd_set_section_size (obfd, osection, size))
  677.             nonfatal (bfd_get_filename (obfd));
  678.         }
  679.  
  680.       if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
  681.                      size))
  682.     {
  683.       nonfatal (bfd_get_filename (obfd));
  684.     }
  685.       free (memhunk);
  686.     }
  687. }
  688.  
  689. /* Mark all the symbols which will be used in output relocations with
  690.    the BSF_KEEP flag so that those symbols will not be stripped.
  691.  
  692.    Ignore relocations which will not appear in the output file.  */
  693.  
  694. static void
  695. mark_symbols_used_in_relocations (ibfd, isection, symbols)
  696.      bfd *ibfd;
  697.      sec_ptr isection;
  698.      asymbol **symbols;
  699. {
  700.   long relsize;
  701.   arelent **relpp;
  702.   long relcount, i;
  703.  
  704.   /* Ignore an input section with no corresponding output section.  */
  705.   if (isection->output_section == NULL)
  706.     return;
  707.  
  708.   relsize = bfd_get_reloc_upper_bound (ibfd, isection);
  709.   if (relsize < 0)
  710.     bfd_fatal (bfd_get_filename (ibfd));
  711.  
  712.   relpp = (arelent **) xmalloc (relsize);
  713.   relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
  714.   if (relcount < 0)
  715.     bfd_fatal (bfd_get_filename (ibfd));
  716.  
  717.   /* Examine each symbol used in a relocation.  If it's not one of the
  718.      special bfd section symbols, then mark it with BSF_KEEP.  */
  719.   for (i = 0; i < relcount; i++)
  720.     {
  721.       if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
  722.       && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
  723.       && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
  724.     (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
  725.     }
  726.  
  727.   if (relpp != NULL)
  728.     free (relpp);
  729. }
  730.  
  731. /* The number of bytes to copy at once.  */
  732. #define COPY_BUF 8192
  733.  
  734. /* Copy file FROM to file TO, performing no translations.
  735.    Return 0 if ok, -1 if error.  */
  736.  
  737. static int
  738. simple_copy (from, to)
  739.      char *from, *to;
  740. {
  741.   int fromfd, tofd, nread;
  742.   char buf[COPY_BUF];
  743.  
  744.   fromfd = open (from, O_RDONLY);
  745.   if (fromfd < 0)
  746.     return -1;
  747.   tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC);
  748.   if (tofd < 0)
  749.     {
  750.       close (fromfd);
  751.       return -1;
  752.     }
  753.   while ((nread = read (fromfd, buf, sizeof buf)) > 0)
  754.     {
  755.       if (write (tofd, buf, nread) != nread)
  756.     {
  757.       close (fromfd);
  758.       close (tofd);
  759.       return -1;
  760.     }
  761.     }
  762.   close (fromfd);
  763.   close (tofd);
  764.   if (nread < 0)
  765.     return -1;
  766.   return 0;
  767. }
  768.  
  769. #ifndef S_ISLNK
  770. #ifdef S_IFLNK
  771. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
  772. #else
  773. #define S_ISLNK(m) 0
  774. #define lstat stat
  775. #endif
  776. #endif
  777.  
  778. /* Rename FROM to TO, copying if TO is a link.
  779.    Assumes that TO already exists, because FROM is a temp file.
  780.    Return 0 if ok, -1 if error.  */
  781.  
  782. static int
  783. smart_rename (from, to)
  784.      char *from, *to;
  785. {
  786.   struct stat s;
  787.   int ret = 0;
  788.  
  789.   if (lstat (to, &s))
  790.     return -1;
  791.  
  792.   /* Use rename only if TO is not a symbolic link and has
  793.      only one hard link.  */
  794.   if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
  795.     {
  796.       ret = rename (from, to);
  797.       if (ret == 0)
  798.     {
  799.       /* Try to preserve the permission bits and ownership of TO.  */
  800.       chmod (to, s.st_mode & 07777);
  801.       chown (to, s.st_uid, s.st_gid);
  802.     }
  803.     }
  804.   else
  805.     {
  806.       ret = simple_copy (from, to);
  807.       if (ret == 0)
  808.     unlink (from);
  809.     }
  810.   return ret;
  811. }
  812.  
  813. static int
  814. strip_main (argc, argv)
  815.      int argc;
  816.      char *argv[];
  817. {
  818.   char *input_target = NULL, *output_target = NULL;
  819.   boolean show_version = false;
  820.   int c, i;
  821.  
  822.   while ((c = getopt_long (argc, argv, "I:O:F:R:sSgxXVv",
  823.                strip_options, (int *) 0)) != EOF)
  824.     {
  825.       switch (c)
  826.     {
  827.     case 'I':
  828.       input_target = optarg;
  829.       break;
  830.     case 'O':
  831.       output_target = optarg;
  832.       break;
  833.     case 'F':
  834.       input_target = output_target = optarg;
  835.       break;
  836.     case 'R':
  837.       {
  838.         struct section_list *n;
  839.  
  840.         n = (struct section_list *) xmalloc (sizeof (struct section_list));
  841.         n->name = optarg;
  842.         n->next = remove_sections;
  843.         remove_sections = n;
  844.       }
  845.       break;
  846.     case 's':
  847.       strip_symbols = strip_all;
  848.       break;
  849.     case 'S':
  850.     case 'g':
  851.       strip_symbols = strip_debug;
  852.       break;
  853.     case 'x':
  854.       discard_locals = locals_all;
  855.       break;
  856.     case 'X':
  857.       discard_locals = locals_start_L;
  858.       break;
  859.     case 'v':
  860.       verbose = true;
  861.       break;
  862.     case 'V':
  863.       show_version = true;
  864.       break;
  865.     case 0:
  866.       break;        /* we've been given a long option */
  867.     case 'h':
  868.       strip_usage (stdout, 0);
  869.     default:
  870.       strip_usage (stderr, 1);
  871.     }
  872.     }
  873.  
  874.   if (show_version)
  875.     {
  876.       printf ("GNU %s version %s\n", program_name, program_version);
  877.       exit (0);
  878.     }
  879.  
  880.   /* Default is to strip all symbols.  */
  881.   if (strip_symbols == strip_undef && discard_locals == locals_undef)
  882.     strip_symbols = strip_all;
  883.  
  884.   if (output_target == (char *) NULL)
  885.     output_target = input_target;
  886.  
  887.   i = optind;
  888.   if (i == argc)
  889.     strip_usage (stderr, 1);
  890.  
  891.   for (; i < argc; i++)
  892.     {
  893.       int hold_status = status;
  894.  
  895.       char *tmpname = make_tempname (argv[i]);
  896.       status = 0;
  897.       copy_file (argv[i], tmpname, input_target, output_target);
  898.       if (status == 0)
  899.     {
  900.       smart_rename (tmpname, argv[i]);
  901.       status = hold_status;
  902.     }
  903.       else
  904.     unlink (tmpname);
  905.       free (tmpname);
  906.     }
  907.  
  908.   return 0;
  909. }
  910.  
  911. static int
  912. copy_main (argc, argv)
  913.      int argc;
  914.      char *argv[];
  915. {
  916.   char *input_filename = NULL, *output_filename = NULL;
  917.   char *input_target = NULL, *output_target = NULL;
  918.   boolean show_version = false;
  919.   int c;
  920.  
  921.   while ((c = getopt_long (argc, argv, "b:i:I:s:O:d:F:R:SgxXVv",
  922.                copy_options, (int *) 0)) != EOF)
  923.     {
  924.       switch (c)
  925.     {
  926.     case 'b':
  927.       copy_byte = atoi(optarg);
  928.       if (copy_byte < 0)
  929.         {
  930.           fprintf (stderr, "%s: byte number must be non-negative\n",
  931.                program_name);
  932.           exit (1);
  933.         }
  934.       break;
  935.     case 'i':
  936.       interleave = atoi(optarg);
  937.       if (interleave < 1)
  938.         {
  939.           fprintf(stderr, "%s: interleave must be positive\n",
  940.               program_name);
  941.           exit (1);
  942.         }
  943.       break;
  944.     case 'I':
  945.     case 's':        /* "source" - 'I' is preferred */
  946.       input_target = optarg;
  947.       break;
  948.     case 'O':
  949.     case 'd':        /* "destination" - 'O' is preferred */
  950.       output_target = optarg;
  951.       break;
  952.     case 'F':
  953.       input_target = output_target = optarg;
  954.       break;
  955.     case 'R':
  956.       {
  957.         struct section_list *n;
  958.  
  959.         n = (struct section_list *) xmalloc (sizeof (struct section_list));
  960.         n->name = optarg;
  961.         n->next = remove_sections;
  962.         remove_sections = n;
  963.       }
  964.       break;
  965.     case 'S':
  966.       strip_symbols = strip_all;
  967.       break;
  968.     case 'g':
  969.       strip_symbols = strip_debug;
  970.       break;
  971.     case 'x':
  972.       discard_locals = locals_all;
  973.       break;
  974.     case 'X':
  975.       discard_locals = locals_start_L;
  976.       break;
  977.     case 'v':
  978.       verbose = true;
  979.       break;
  980.     case 'V':
  981.       show_version = true;
  982.       break;
  983.     case 0:
  984.       break;        /* we've been given a long option */
  985.     case 'h':
  986.       copy_usage (stdout, 0);
  987.     default:
  988.       copy_usage (stderr, 1);
  989.     }
  990.     }
  991.  
  992.   if (show_version)
  993.     {
  994.       printf ("GNU %s version %s\n", program_name, program_version);
  995.       exit (0);
  996.     }
  997.  
  998.   if (copy_byte >= interleave)
  999.     {
  1000.       fprintf (stderr, "%s: byte number must be less than interleave\n",
  1001.            program_name);
  1002.       exit (1);
  1003.     }
  1004.  
  1005.   if (optind == argc || optind + 2 < argc)
  1006.     copy_usage (stderr, 1);
  1007.  
  1008.   input_filename = argv[optind];
  1009.   if (optind + 1 < argc)
  1010.     output_filename = argv[optind + 1];
  1011.  
  1012.   /* Default is to strip no symbols.  */
  1013.   if (strip_symbols == strip_undef && discard_locals == locals_undef)
  1014.     strip_symbols = strip_none;
  1015.  
  1016.   if (output_target == (char *) NULL)
  1017.     output_target = input_target;
  1018.  
  1019.   /* If there is no destination file then create a temp and rename
  1020.      the result into the input.  */
  1021.  
  1022.   if (output_filename == (char *) NULL)
  1023.     {
  1024.       char *tmpname = make_tempname (input_filename);
  1025.       copy_file (input_filename, tmpname, input_target, output_target);
  1026.       if (status == 0)
  1027.     smart_rename (tmpname, input_filename);
  1028.       else
  1029.     unlink (tmpname);
  1030.     }
  1031.   else
  1032.     {
  1033.       copy_file (input_filename, output_filename, input_target, output_target);
  1034.     }
  1035.  
  1036.   return 0;
  1037. }
  1038.  
  1039. int
  1040. main (argc, argv)
  1041.      int argc;
  1042.      char *argv[];
  1043. {
  1044.   program_name = argv[0];
  1045.   xmalloc_set_program_name (program_name);
  1046.   strip_symbols = strip_undef;
  1047.   discard_locals = locals_undef;
  1048.  
  1049.   bfd_init ();
  1050.  
  1051.   if (is_strip < 0)
  1052.     {
  1053.       int i = strlen (program_name);
  1054.       is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip"));
  1055.     }
  1056.  
  1057.   if (is_strip)
  1058.     strip_main (argc, argv);
  1059.   else
  1060.     copy_main (argc, argv);
  1061.  
  1062.   return status;
  1063. }
  1064.